﻿<cfcomponent>

<cfscript>
DB = 'chinalaohu';
</cfscript>
<cfparam name="UUID" default="0" type="string" >






<!---管理银行账号接口--->
<cffunction name="list" access="remote" returnFormat="json">
  <cfscript>
	j = {"success":"0"};
	address = arraynew(1);
	</cfscript>
	<!---根据UUID查找ID--->
  <cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" UUID="#uuid#"/>
  <cfif uuidgetuid.recordcount neq 1>
  	<!---,"msg":"用户ID错误"--->
    <cfset j = {"success":"0","errCode":"0"}>
  <cfelse>
  	<cfinvoke method="userbanklist" returnvariable="userbanklist" UID="#uuidgetuid.uid#"/>
    <cfif userbanklist.recordcount gt 0>
    	<cfoutput query="userbanklist">
				<cfscript>
        listjson = {
        "type":set_default,
				"aid":ubid,
        "name":cardname,
				"card":cardno,
        "province":province,
				"city": city,
        "bank":bank,
				"contact":mobile
        };				
        arrayappend(address,listjson);
        </cfscript>
      </cfoutput>
    </cfif>
    <cfscript>
		j = {"success":"1","address":address};
		</cfscript>
  
  </cfif>

  
  <cfscript>
	j = createObject("component",'cfjson').encode(j);
  return j;
	</cfscript>

</cffunction>


<!---新增银行账号接口--->
<cffunction name="add" access="remote" returnFormat="json">

  <cfscript>
	j = {"success":"0"};
	</cfscript>
	<!---根据UUID查找ID--->
  <cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" UUID="#uuid#"/>
  <cfif uuidgetuid.recordcount eq 0>
  	<!---,"msg":"用户ID错误"--->
    <cfset j = {"success":"0","errCode":"0"}>
  <cfelse>

  	<cfif isdefined('name') AND isdefined('card') and isdefined('province') and isdefined('city') and isdefined('bank') and isdefined('contact')>
  	<cfinvoke method="usernewbank" component="app.cfc.userbank" returnvariable="newaid" UID="#uuidgetuid.uid#"/>
    <cfscript>
		j = {"success":"1","aid":newaid};
		</cfscript>
    
    <cfelse>
    <cfscript>
		//,"msg":"缺少参数"
		j = {"success":"0","errCode":"-1"};
		</cfscript>
    
    </cfif>
  
  </cfif>
  <cfscript>
	j = createObject("component",'cfjson').encode(j);
  return j;
	</cfscript>


</cffunction>


<!---修改银行账号接口--->
<cffunction name="edit" access="remote" returnFormat="json">
  <cfscript>
	j = {"success":"0"};
	</cfscript>
	<!---根据UUID查找ID--->
  <cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" UUID="#uuid#"/>
  <cfif uuidgetuid.recordcount eq 0>
  	<!---,"msg":"用户ID错误"--->
    <cfset j = {"success":"0","errCode":"0"}>
  <cfelse>
  	<cfif isdefined('aid') and isdefined('name') AND isdefined('card') and isdefined('province') and isdefined('city') and isdefined('bank') and isdefined('contact')>
      <cfinvoke method="showbank" returnvariable="showbank" UID="#uuidgetuid.uid#" ubid="#aid#"/>
      <cfif showbank.recordcount gt 0>
      
        <cfinvoke method="usernewedit" returnvariable="modaid" UID="#uuidgetuid.uid#"/>
        <cfscript>
        j = {"success":"1","aid":modaid};
        </cfscript>
      
      <cfelse>
				<cfscript>
				//,"msg":"操作过期"
        j = {"success":"0","errCode":"-1"};
        </cfscript>
      </cfif>

    <cfelse>
			<cfscript>
			//,"msg":"缺少参数"
      j = {"success":"0","errCode":"-1"};
      </cfscript>
      
    </cfif>
  
  </cfif>
  
  <cfscript>
	j = createObject("component",'cfjson').encode(j);
  return j;
	</cfscript>

</cffunction>

<!---删除银行账号接口--->
<cffunction name="del" access="remote" returnFormat="json">
<!---<cfquery datasource="#DB#">
insert into tmp(post,act,dt)
value('#serializeJSON(form)#','LIST',#now()#)
</cfquery>--->
  <cfscript>
	j = {"success":"0"};
	</cfscript>
	<!---根据UUID查找ID--->
  <cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" UUID="#uuid#"/>
  
  <cfif uuidgetuid.recordcount eq 0>
  	<!---,"msg":"用户ID错误"--->
    <cfset j = {"success":"0","errCode":"0"}>
  <cfelse>
  	<cfif isdefined('aid')>
      <cfinvoke method="showbank" returnvariable="showbank" UID="#uuidgetuid.uid#" ubid="#aid#"/>
      <cfif showbank.recordcount gt 0>


        <!---删除--->
        <cfquery datasource="#DB#">
        DELETE FROM user_bank
        WHERE ubid = #aid# AND uid = '#uuidgetuid.uid#'
        </cfquery>
        
        <cfscript>
        j = {"success":"1"};
        </cfscript>
      
      <cfelse>
				<cfscript>
				//,"msg":"删除操作过期"
        j = {"success":"0","errCode":"-1"};
        </cfscript>
      </cfif>
      
    <cfelse>
			<cfscript>
			//,"msg":"缺少参数"
      j = {"success":"0","errCode":"-1"};
      </cfscript>
      
    </cfif>
  
  </cfif>
  
  <cfscript>
	j = createObject("component",'cfjson').encode(j);
  return j;
	</cfscript>

</cffunction>





<!---检测--->
<cffunction access="public" name="showbank" returntype="query">
<cfquery datasource="#DB#" name="showbank">
select *
from user_bank
where uid = '#uid#' AND ubid = #ubid#
</cfquery>
<cfreturn showbank>
</cffunction>



<!---修改银行--->
<cffunction access="public" name="usernewedit" returntype="any">
	<cfquery datasource="#DB#">
  UPDATE user_bank SET
  <cfif isdefined('type') and (type eq 1 or type eq 0)>set_default = #type#,</cfif>
  cardname = '#name#',
  province = '#province#',
  city = '#city#',
  bank = '#bank#',
  cardno = '#card#',
  mobile = '#contact#'
  WHERE ubid = #aid# and UID = #UID#
  </cfquery>
  <cfif isdefined('type') and type eq 1>
  <cfquery datasource="#DB#">
  UPDATE user_bank SET
  set_default = 0
  WHERE ubid <> #aid# and UID = #UID# AND set_default = 1
  </cfquery>
	</cfif>
  <cfreturn aid>
</cffunction>


<!---新银行--->
<cffunction access="public" name="usernewbank" returntype="any">
	<cfquery datasource="#DB#" result="addrs">
  insert into user_bank(uid,cardname,province,city,bank,cardno,mobile)
  value(#uid#,'#name#','#province#','#city#','#bank#','#card#','#contact#')
  </cfquery>
  <!---地址唯一设置默认--->
	<cfquery datasource="#DB#" name="checkuseraddrrc">
  select ubid
  from user_bank
  where uid = #uid#
  </cfquery>
  <cfif checkuseraddrrc.recordcount eq 1>
    <cfquery datasource="#DB#">
    update user_bank set
    set_default = 1
    where uid = #uid#
    </cfquery>
  <cfelseif type eq 1>
  	<cfquery datasource="#DB#">
    update user_bank set
    set_default = 0
    where uid = #uid# and set_default > 0
    </cfquery>
  
  	<cfquery datasource="#DB#">
    update user_bank set
    set_default = 1
    where uid = #uid# AND ubid = #addrs.GENERATEDKEY#
    </cfquery>
	</cfif>
  <cfreturn addrs.GENERATEDKEY>
</cffunction>




<!---地址列表--->
<cffunction access="public" name="userbanklist" returntype="query">
<cfquery datasource="#DB#" name="userbanklist">
select *
from user_bank
where uid = #uid#
order by set_default desc
</cfquery>
<cfreturn userbanklist>
</cffunction>

<!---默认银行--->
<cffunction access="public" name="defaultbank" returntype="query">
<cfquery datasource="#DB#" name="showbank">
select *
from user_bank
where uid = '#uid#' AND set_default = 1
</cfquery>
<cfreturn showbank>
</cffunction>


  
</cfcomponent>

